package com.fyyg.common.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by fy on 2016/7/21.
 */
public class ShardingMonthUtil {

    public  static Integer currentActiveShardingKey = 1000;

    public static Integer currentShardingMonth(){
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH)+1;
        return Integer.valueOf(year.toString().substring(2)+""+(month < 10 ? "0"+month : month));
    }

    public static Integer nextShardingMonth(){
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH)+2;
        return Integer.valueOf(year.toString().substring(2)+""+(month < 10 ? "0"+month : month));
    }

    public static Integer lastShardingMonth(){
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH);
        return Integer.valueOf(year.toString().substring(2)+""+(month < 10 ? "0"+month : month));
    }

    public static Integer getShardingMonthByTimeStamp(Long timeStamp){
        Calendar calendar= Calendar.getInstance();
        timeStamp = timeStamp.toString().length() < 13 ? timeStamp*1000 : timeStamp;
        calendar.setTime(new Timestamp(timeStamp));
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH)+1;
        return Integer.valueOf(year.toString().substring(2)+""+(month < 10 ? "0"+month : month));
    }

    public static Integer getShardingMonthByTimeStamp(String timeStamp){
       return getShardingMonthByTimeStamp(Long.valueOf(timeStamp));
    }
    
    public static List<Integer> yearsFullShardingMonth(){
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        List<Integer> list = new ArrayList<>(12);
        for (int i = 1; i <= 12; i++) {
        	list.add(Integer.valueOf(year.toString().substring(2)+""+(i < 10 ? "0"+i : i)));
		}
        return list;
    }
    
    public static List<Integer> lastYearFullShardingMonth(){
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR)-1;
        List<Integer> list = new ArrayList<>(12);
        for (int i = 1; i <= 12; i++) {
        	list.add(Integer.valueOf(year.toString().substring(2)+""+(i < 10 ? "0"+i : i)));
		}
        return list;
    }
    
    public static Integer getShardingMonthBySeqNo(String seqNo){
    	if(seqNo == null){
    		throw new RuntimeException("seqNo is null");
    	}
    	if(seqNo.length() <= 12){
    		throw new RuntimeException("seqNo lenth must greater than 12");
    	}
    	String baseSqe = seqNo.substring(0, 12);
    	if(baseSqe.contains(currentShardingMonth().toString())){
    		return currentShardingMonth();
    	}
    	if(baseSqe.contains(currentShardingMonth().toString())){
    		return currentShardingMonth();
    	}
    	
    	List<Integer> yearShardingList = yearsFullShardingMonth();
    	for (Integer integer : yearShardingList) {
        	if(baseSqe.contains(integer.toString())){
        		return integer;
        	}
		}
    	
    	yearShardingList = lastYearFullShardingMonth();
    	for (Integer integer : yearShardingList) {
        	if(baseSqe.contains(integer.toString())){
        		return integer;
        	}
		}
    	throw new RuntimeException("seqNo format is error or seqNo is too old");
    	
    }



    public static void main(String[] args) {
       System.out.println(getShardingMonthByTimeStamp("1469102400"));
    }

}
